#!/usr/bin/env python3
#  -*- coding: utf-8 -*-

from tkinter import *
import os

def get_process_pid(name):
    child = os.popen("ps -ef | grep "+name).readline()
    response = child.split(' ')
    print(response)
    for i in range(2,20):
        if response[i] != "":
            pid_str = response[i]
            break
        else:
            pass
    return pid_str 
#=======================================================================
def killalm6():
    pid = get_process_pid("Scan_6_ch_Alarm")
    os.system('sudo kill -s 15 '+pid)  #先发送TERM信号终止进程，以正常关闭蜂鸣器
    pid = get_process_pid("Scan_6_ch_Alarm")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_6_ch_Alarm")
    os.system('sudo kill -s 9 '+pid)
    
def killalm3():
    pid = get_process_pid("Scan_3_ch_Alarm")
    os.system('sudo kill -s 15 '+pid)  #先发送TERM信号终止进程，以正常关闭蜂鸣器
    pid = get_process_pid("Scan_3_ch_Alarm")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_3_ch_Alarm")
    os.system('sudo kill -s 9 '+pid)
    
def killrec6():
    pid = get_process_pid("Scan_6_ch_rec")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_6_ch_rec")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_6_ch_rec")
    os.system('sudo kill -s 9 '+pid)

def killrec3():
    pid = get_process_pid("Scan_3_ch_rec")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_3_ch_rec")
    os.system('sudo kill -s 9 '+pid)
    pid = get_process_pid("Scan_3_ch_rec")
    os.system('sudo kill -s 9 '+pid)
#=======================================================================    
def runalm6():
    scan_rate = entry1.get()
    read_req_size = entry2.get()
    volt1 = entry3.get()
    volt2 = entry4.get()
    os.system('/home/pi/dist/Scan_6_ch_Alarm.py'+' rate'+scan_rate+' size'+read_req_size+' volt'+ volt1+' thrd'+volt2+' &')
    
def runalm3():
    scan_rate = entry1.get()
    read_req_size = entry2.get()
    volt1 = entry3.get()
    volt2 = entry4.get()
    os.system('/home/pi/dist/Scan_3_ch_Alarm.py'+' rate'+scan_rate+' size'+read_req_size+' volt'+ volt1+' thrd'+volt2+' &')
    
def runrec6():
    scan_rate = entry1.get()
    read_req_size = entry2.get()
    volt1 = entry3.get()
    volt2 = entry4.get()
    os.system('/home/pi/dist/Scan_6_ch_rec.py'+' rate'+scan_rate+' size'+read_req_size+' volt'+ volt1+' thrd'+volt2+' &')
    
def runrec3():
    scan_rate = entry1.get()
    read_req_size = entry2.get()
    volt1 = entry3.get()
    volt2 = entry4.get()
    os.system('/home/pi/dist/Scan_3_ch_rec.py'+' rate'+scan_rate+' size'+read_req_size+' volt'+ volt1+' thrd'+volt2+' &')
#=======================================================================
def main():
    global entry1, entry2, entry3, entry4
    #定义为全局变量
    #初始化Tk()
    myWindow = Tk()
    #设置标题
    myWindow.title('三相交流电监测及告警')
    #设置窗口大小
    width = 720
    height = 450
    #获取屏幕尺寸以计算布局参数，使窗口居屏幕中央
    screenwidth = myWindow.winfo_screenwidth()      #获取屏幕宽度
    screenheight = myWindow.winfo_screenheight()    #获取屏幕高度
    alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)  #生成屏幕位置，大小参数
    myWindow.geometry(alignstr)     #按位置绘制屏幕
    #设置窗口是否可变长、宽，True：可变，False：不可变
    myWindow.resizable(width=False, height=False)            #敞口的高宽是否可以调整
#======第一行====================================================================================================
    l3 = Label(myWindow, text="采样率", font=('KaiTi', 12, 'bold'), fg='blue', width=5, height=2)
    l3.grid(row=0, column=0,  padx=5, pady=5,sticky="e")
    entry1 = Entry(myWindow, bd=3, width=7)
    entry1.grid(row=0, column=1,sticky="w")

    l4 = Label(myWindow, text="采样数", font=('KaiTi', 12, 'bold'),  fg='blue', width=5, height=2)
    l4.grid(row=0, column=2,  padx=5, pady=5,sticky="e")
    entry2 = Entry(myWindow, bd=3, width=7)
    entry2.grid(row=0, column=3,sticky="w")

    l5 = Label(myWindow, text="电压上限", font=('KaiTi', 12, 'bold'),  fg='blue', width=8, height=2)
    l5.grid(row=0, column=4,  padx=5, pady=5,sticky="e")
    entry3 = Entry(myWindow, bd=3, width=5)
    entry3.grid(row=0, column=5,sticky="w")

    l6 = Label(myWindow, text="电压下限", font=('KaiTi', 12, 'bold'),  fg='blue', width=8, height=2)
    l6.grid(row=0, column=6,  padx=5, pady=5,sticky="e")
    entry4 = Entry(myWindow, bd=3, width=5)
    entry4.grid(row=0, column=7,sticky="w")

    entry1.insert(0, 3600)   # scan_rate
    entry2.insert(0, 3600)   # request_size
    entry3.insert(0, 264)    # volt-up
    entry4.insert(0, 176)    # volt-down
# ======第二行====================================================================================================
    l7= Label(myWindow, text="采样率:最大7200/信道数，最低3600，默认3600；建议3600的倍数，3600、7200；\n"
                             "采样数:最大20000，最低3600，默认3600；建议3600的倍数：3600、7200、10800；\n"
                             "电压上限默认264V,有效范围(240V-300V)，电压下限默认176V，有效范围(120V-200V)。"
                            , font=('KaiTi', 10, 'bold'), bg='moccasin', fg='blue', width=75, height=3)
    l7.grid(row=1, column=0, columnspan=8, padx=5, pady=5)
# ======第三、四行====================================================================================================
    b1=Button(myWindow, text='终止6CH\n带告警',font=('KaiTi',15,'bold'), command=killalm6,fg='red',width=8, height=2)        #绘制按钮
    b1.grid(row=3, column=0, padx=5,pady=5,columnspan=2)
    b2=Button(myWindow, text='运行6CH\n带告警',font=('KaiTi',15,'bold'),fg='blue',command=runalm6,width=8, height=2)
    b2.grid(row=2, column=0,  padx=5, pady=5,columnspan=2)

    b3=Button(myWindow, text='终止3CH\n带告警',font=('KaiTi',15,'bold'), command=killalm3,fg='red',width=8, height=2)        #绘制按钮
    b3.grid(row=3, column=2, padx=5,pady=5,columnspan=2)
    b4=Button(myWindow, text='运行3CH\n带告警',font=('KaiTi',15,'bold'),fg='blue',command=runalm3,width=8, height=2)
    b4.grid(row=2, column=2,  padx=5, pady=5,columnspan=2)
    
    b5=Button(myWindow, text='终止6CH\n长期监测',font=('KaiTi',15,'bold'), command=killrec6,fg='red',width=8, height=2)        #绘制按钮
    b5.grid(row=3, column=4,  padx=5,pady=5,columnspan=2)
    b6=Button(myWindow, text='运行6CH\n长期监测',font=('KaiTi',15,'bold'),fg='blue',command=runrec6,width=8, height=2)
    b6.grid(row=2, column=4, padx=5, pady=5,columnspan=2)
    
    b7=Button(myWindow, text='终止3CH\n长期监测',font=('KaiTi',15,'bold'), command=killrec3,fg='red',width=8, height=2)        #绘制按钮
    b7.grid(row=3, column=6,  padx=5,pady=5,columnspan=2)
    b8=Button(myWindow, text='运行3CH\n长期监测',font=('KaiTi',15,'bold'),fg='blue',command=runrec3,width=8, height=2)
    b8.grid(row=2, column=6,  padx=5, pady=5,columnspan=2)
# ======第五行====================================================================================================
    l1 = Label(myWindow, text="监测两个变压器，请使用6CH，监测一个变压器，请使用3CH。\n"
                              "如需实时告警，请使用\"带告警\",如需长期监测，请使用\"长期监测\"。\n"
                              "如运行长期监测请务必连接硬盘，硬盘的名称应为\"Seagate\"，\n"
                              "且根目录建有”data“文件夹。注意：长期监测电压上下限不起作用。\n"
                              , font=('KaiTi',12,'bold'), bg='wheat', fg='blue', width=65, height=5)
    l1.grid(row=4, column=0, columnspan=8, padx=5, pady=5)
# ======第六行====================================================================================================
    l2=Label(myWindow, text="注意：一次只能运行一个程序，如需要运行其他，请先终止运行的程序。", font=('KaiTi',12,'bold'), bg='yellow', fg='red', width=65, height=2)
    l2.grid(row=5, column=0, columnspan=8, padx=5, pady=5)



    #进入消息循环
    myWindow.mainloop()
    
if __name__ == '__main__':
    main()
   
        

